package com.foreknow.service.impl;

import com.foreknow.dao.GuestbookDao;
import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.util.Pagination;
import com.sun.org.apache.bcel.internal.generic.DMUL;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class GuestbookServiceImpl implements GuestbookService {
    private GuestbookDaoImpl dao = new GuestbookDaoImpl();

    @Override
    public boolean add(Guestbook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = dao.save(gb);
            //dml操作需要提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public List<Object> showAll() {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);
        List<Object> queryAll = null;
        try {
            queryAll = dao.getQueryAll();
            return queryAll;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


//
    @Override
    public boolean deleteById(int id) {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);

        try {
            boolean b = dao.deleteById(id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Guestbook getQueryById(int id) {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);
        try {
            Guestbook queryById = dao.getQueryById(id);
            return queryById;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean update(Guestbook gb) {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);
        try {
            boolean update = dao.update(gb);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Pagination getByPage(int pageSize, int pageNumber) {
        DBManager instance = DBManager.getInstance();
        Connection conn = instance.getConnection();
        dao.setConnection(conn);
        try {
            //业务层调用持久层
            Pagination guestbookByPage = dao.getGuestbookByPage(pageSize, pageNumber);
            return guestbookByPage;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}
